/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "file_manager.h" // Include the header defining IFileManager
#include <string>
#include <vector>
#include <map>
#include <mutex>
#include <atomic>
#include <memory> // For unique_ptr
#include "nlohmann/json.hpp"
#include "file_index.h"
#include "utils_noncopyable.h"

namespace El {
namespace FileManager {

class FileManagerImpl : public IFileManager {
    DISALLOW_COPY_AND_MOVE(FileManagerImpl); // Keep this for the implementation class

public:
    // Constructor and Destructor
    FileManagerImpl();
    ~FileManagerImpl() override; // Use override

    // Implement interface methods
    bool Start() override;
    std::vector<FileInfo> GetFileList(FileType type, bool includeDeleted) override;
    bool GetFileInfo(const std::string &fileId, FileInfo &outInfo) override;
    bool GetFileInfoByName(const std::string &fileName, FileInfo &outInfo) override;
    bool DeleteFile(const std::string &fileId) override;
    bool RestoreFile(const std::string &fileId) override;
    std::string CreateFile(const std::string &fileName, FileType type) override;
    bool WriteFileBlock(const std::string &fileId, const std::string &block, bool isLastBlock) override;
    bool GetStorageInfo(int32_t &totalSpace, int32_t &usedSpace, int32_t &availableSpace) override;
    std::string GetFilePath(const std::string &fileId) override;
    bool ReadFileBlock(const std::string &fileId, int chunkIndex, int32_t chunkSize, std::string &outData) override;

private:
    // Private helper methods (remain the same)
    bool CheckStorageSpace(int32_t requiredSpace);
    bool CheckFileTypeLimit(FileType type);
    bool EnsureDirectoryExists(const std::string &path);
    std::string GetTempFilePath(const std::string &fileId);
    std::string GetFinalFilePath(const std::string &fileName, FileType type);
    bool MoveToFinalPath(const std::string &tempPath, const std::string &finalPath);
    void InitConfig();

    // Private members (remain the same)
    std::map<std::string, std::string> activeFiles_; // fileId -> tempPath
    std::mutex activeFilesMutex_;
    std::unique_ptr<FileIndex> fileIndex_;
    std::string presetDir_;
    std::string userDir_;
    // std::string indexDir_; // Removed as per previous logic
    int32_t maxFilesPerType_{100};
    std::atomic<bool> initialized_{false}; // Added to manage initialization state for singleton
};

} // namespace FileManager
} // namespace El